package nocuffin.m5;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;

import android.os.Bundle;

import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;



/** 
 * Team: No Cuffin'
 * 2/27/2012
 * 
 *
 * This will be our MainUI!!
 * 
 * 
 * @author Robert Allen, Philip Johnson, Steven Moore
 * @version 1.0 2/27/2012
 *
 */ 
public class TaskView extends ListActivity{

	
	String contents[], myList[], pielist[], tryList[];
	String collected;
	String name, entries, criteria;
	boolean special, filteractive;
	ArrayList<String> displayList;
	String[] returnarray;
	 //This is our username...which we must pass continuously
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		Bundle b = getIntent().getExtras(); 
		String name = b.getString("name");
		special = false;
		filteractive = false;
		if (b.getString("criteria") != null)
		{
			criteria = b.getString("criteria");
			special  = b.getBoolean("special");
			filteractive = true;
		}
		
		//Need to populate myList...but conditionally based on results of text file search
		this.name = name;
		entries = findEntries();
		this.myList = new String[20];
		this.myList = createNameList(entries, special).clone();
		
		
		if (returnarray.length < 2 && !(special))
		{
			this.pielist = new String[1];
			pielist[0] = "+ New Entry";
		}
		else if ((returnarray.length < 2) && (special))
		{
			this.pielist = new String[1];
			pielist[0] = "+ Reset Filter";
		}
		else
		{
			this.pielist = new String[returnarray.length + 2];
			if (filteractive || b.getString("criteria") != null)
			{
				pielist[0] = "+ Reset Filter"; // FIX RETURN SO THAT IF FILTER IS ACTIVE TAKES FILTER OFF
			}
			else
			{
				pielist[0] = "+ New Entry";
			}
			pielist[1] = "+ Apply Filter";
			pielist[2] = "+ Go Back";
			for ( int x =3; x < returnarray.length + 2; x++)
			{
				pielist[x] = returnarray[x - 3];
			}
			
		}
		
		
		
			setListAdapter(new ArrayAdapter<String>(TaskView.this, android.R.layout.simple_list_item_1, pielist));	
		
	}
		
	
	
	
	String[] createNameList(String lol, boolean i){
		
		
		String[] entriesfun;
		
		entriesfun = lol.split(":");
		
		displayList = new ArrayList<String>();
		if (entriesfun != null)
		{
		 for (int x = 0; x < entriesfun.length; x++)
         {
			
				 displayList.add(entriesfun[x]);
			 
         }
		}
		
		 int size = displayList.size();
		 size = size / 6;
		 //displayList has EVERy entries between :'s!!
		 //Will be enough for each task descrip...and add new!
		 returnarray = new String[size + 1];
		 //lets say decided to have 3 entries
		 int count = 0;
		 //displayList.size() is number of entries in displayList...all :-seperated entries and starting at new
		if (displayList.size() > 1)
		{
		 for (int x = 0; x < displayList.size(); x++)
         {
			 
			 if ((x) % 6 == 1)
			 {
				 returnarray[count] = displayList.get(x);
				 count = count + 1;
			 }
			
         }
		}
		 
		
		String[] wrong = {"error"};
		
		if (returnarray.length != 0)
		{
			return returnarray;
		}
		else
		{
			return wrong;
		}
		
	}
	
	@SuppressWarnings("null") 
	String findEntries(){
		//This routine is for reading the full contents of our key file until indicated
		FileInputStream fis = null;
		
		
		try 
		{
			fis = openFileInput("key");
			byte[] dataArray = new byte[fis.available()];
			while (fis.read(dataArray) != -1)
			{
				this.collected = new String(dataArray);
			}
		
		} 
		
		catch (FileNotFoundException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 	
		
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		finally
		{
				try 
				{
					fis.close();
				} 
				catch (IOException e) 
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		//"INDICATED"
		
		 String[] entries = this.collected.split("\n");
		 //String[] entries has the usernames, yes... and the ":" separated strings of activities
		 boolean hasentry = false;
         String fulltasklist = "";
		 //Parse through entries for ones of the form "data:data:data:etc."
		 
         if (entries.length != 0)
         {
         for (int x = 0; x < entries.length; x++)
         {
			//this.name is the username passed...we are checking our entries list for entries with the username and with a length greater than teh username...aka a ":" thing!
         	if (entries[x].contains(this.name) && (entries[x].length() > this.name.length()) && entries[x].contains(":"))
         	{
         		//This means that we have identified the entry that is useful to us
         		hasentry = true;
         		//adding our big long : thing to a large string of other : things...need to sort out here
         		//receive limitations from new class/window...call bool at 1 depending on their existence
         		if (special && (criteria.equals("work") || criteria.equals("personal") || criteria.equals("school")))
         		{
         			filteractive=true;
         			String[] parsed = entries[x].split(":");
         			if (parsed[5].toLowerCase().equals(criteria.toLowerCase()))
         			{
         				fulltasklist += entries[x] + ":";
         			}
         			else
         			{
         				//EMPTY ELSE FOR BETTER READABILITY
         			}
         			
         		}
         		else if (special && (criteria.equals("This task is not complete.")))
         		{
         			filteractive=true;
         			String[] parsed = entries[x].split(":");
         			if (parsed[4].toLowerCase().contains(criteria.toLowerCase()))
         			{
         				fulltasklist += entries[x] + ":";
         			}
         			else
         			{
         				//EMPTY ELSE FOR BETTER READABILITY
         			}
         		}
         		else if (special && (criteria.length() == 10))
         		{
         			filteractive=true;
         			String[] parsed = entries[x].split(":");
         			String[] dateparts = parsed[3].toLowerCase().split("/");
         			String[] critparts = criteria.toLowerCase().split("/");
         			boolean yearsshorter = false, monthsshorter = false, daysshorter = false;
         			if (Integer.parseInt(dateparts[2]) > Integer.parseInt(critparts[2]))
         			{
         				fulltasklist += entries[x] + ":";
         			}
         			if (Integer.parseInt(dateparts[2]) == Integer.parseInt(critparts[2]))
         			{
         					if (Integer.parseInt(dateparts[0]) > Integer.parseInt(critparts[0]))
         					{
         						fulltasklist += entries[x] + ":";
         					}
         					if (Integer.parseInt(dateparts[0]) == Integer.parseInt(critparts[0]))
         					{
         						if (Integer.parseInt(dateparts[1]) > Integer.parseInt(critparts[1]))
             					{
             						fulltasklist += entries[x] + ":";
             					}
         					}
         			
         		}
         			else
         			{
         				
         			}
         		
         				
         		}
         			
         	
         		else
         		{
         			fulltasklist += entries[x] + ":";
         		}
         		//entertain one at a time, figure out via semaphor for each different kind of limitation
         		// SET SEMAPHOR IF IT PASSES, and if it does, do below
         		//OTHERWISE...DO NOT ADD IT (AKA DO NOT SET SEMAPHOR)
         		
         	}
         }
         }
		 
		 return fulltasklist;
	}
	
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		
		super.onListItemClick(l, v, position, id);
		 
		if (position == 0)
		{
			if (pielist[0].equals("+ New Entry"))
			{
			try {
					Class ourClass = Class.forName("nocuffin.m5." + "NewTask" );
					Intent myIntent = new Intent(v.getContext(), ourClass); 
					Bundle b = getIntent().getExtras(); 
    		 	    String name = b.getString("name"); //This is our username...which we must pass continuously
    		        b.putString("name", name);
    		        b.putStringArray("valofStringarray", myList);
    		        //Add the set of extended data to the intent and start it
    		        myIntent.putExtras(b);
					startActivity(myIntent);
				} 
			catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else
			{
				try {
					Class ourClass = Class.forName("nocuffin.m5." + "TaskView");
					Intent myIntent = new Intent(v.getContext(), ourClass);
					Bundle d = new Bundle(); 
    		 	     //This is our username...which we must pass continuously
					String name1 = name;
    		        d.putString("name", name1);
    		        //Add the set of extended data to the intent and start it
    		        myIntent.putExtras(d);
    		        //Add the set of extended data to the intent and start it
					startActivity(myIntent);
				} 
				catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
		}
			
		
		else if (position == 1)
		{
		if (pielist[1].equals("+ Apply Filter"))
		 {
			try {
					Class ourClass = Class.forName("nocuffin.m5." + "ListFilter" );
					Intent myIntent = new Intent(v.getContext(), ourClass); 
					Bundle b = getIntent().getExtras(); 
    		 	    String name = b.getString("name"); //This is our username...which we must pass continuously
    		        b.putString("name", name);
    		        //Add the set of extended data to the intent and start it
    		        myIntent.putExtras(b);
					startActivity(myIntent);
				} 
			catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
			}
			}
		}
		else if (position == 2)
		{
			if (pielist[2].equals("+ Go Back"))
			{
				try {
					Class ourClass = Class.forName("nocuffin.m5." + "MainUI" );
					Intent myIntent = new Intent(v.getContext(), ourClass); 
					Bundle b = getIntent().getExtras(); 
    		 	    String name = b.getString("name"); //This is our username...which we must pass continuously
    		        b.putString("name", name);
    		        //Add the set of extended data to the intent and start it
    		        myIntent.putExtras(b);
					startActivity(myIntent);
				} 
			catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
			}
		}
			
		}
		else
		{
			try 
			{
				Class ourClass = Class.forName("nocuffin.m5." + "TaskContents");
				Intent myIntent = new Intent(v.getContext(), ourClass);
				Bundle b = new Bundle(); 
				b.putString("nameoftask", displayList.get(position - 1 + (5 * (position - 2))  - 6)); //needs one
				b.putString("locationoftask", displayList.get(position - 1 + (5 * (position - 2))+ (1 -6))); //needs 7
				b.putString("dateoftask", displayList.get(position - 1 + (5 * (position - 2))+ (2-6)));
				b.putString("detailsoftask", displayList.get(position - 1  + (5 * (position - 2))+ (3-6)));
				b.putString("typeoftask", displayList.get(position - 1  + (5 * (position - 2))+ (4-6)));
				b.putString("username", this.name);
				myIntent.putExtras(b);
				startActivity(myIntent);
			} 
			catch (ClassNotFoundException e) 
			{
			
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}
			
	}
	
	
}

	
	
	
	

